package com.wendyxie.sproutup.view

import android.content.Intent
import android.os.Bundle
import android.text.Editable
import android.text.InputFilter
import android.text.InputFilter.LengthFilter
import android.text.InputType
import android.text.TextWatcher
import android.view.KeyEvent
import android.view.View
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.marginEnd
import androidx.core.view.setMargins
import com.wendyxie.sproutup.databinding.ActivityParentPwdBinding
import com.wendyxie.sproutup.utils.DimensionUtils


class ParentPwdActivityBack : AppCompatActivity() {
    private lateinit var binding: ActivityParentPwdBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityParentPwdBinding.inflate(layoutInflater)
        setContentView(binding.root)

        // 动态创建EditText
        val passwordLength = 4
        val editTexts = arrayOfNulls<EditText>(passwordLength)
        for (i in 0 until passwordLength) {
            val editText = EditText(this)
            editText.inputType = InputType.TYPE_CLASS_NUMBER // 可以根据需要设置输入类型
            editText.isSingleLine = true
            editText.maxLines = 1
            editText.setPadding(
                DimensionUtils.dp2px(this, 32F),
                DimensionUtils.dp2px(this, 16F),
                DimensionUtils.dp2px(this, 32F),
                DimensionUtils.dp2px(this, 16F)
            )
            val layoutParams = LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
            )
            layoutParams.weight = 1.0f
            layoutParams.setMargins(
                DimensionUtils.dp2px(this, 16F),
                DimensionUtils.dp2px(this, 16F),
                DimensionUtils.dp2px(this, 16F),
                DimensionUtils.dp2px(this, 16F)
            )
            editText.layoutParams = layoutParams
            editText.filters = arrayOf<InputFilter>(LengthFilter(1)) // 限制输入长度为1
            editText.gravity = View.TEXT_ALIGNMENT_CENTER

            editTexts[i] = editText
            editText.setOnKeyListener { view, j, keyEvent ->
                if (keyEvent.keyCode == KeyEvent.KEYCODE_DEL) {
                    if (editText.text.isEmpty() && i > 0) {
                        editTexts[i - 1]?.requestFocus() // 跳转到上一个输入框
                    }
                };
                return@setOnKeyListener false
            }

            // 添加监听器
            editText.addTextChangedListener(object : TextWatcher {
                override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {

                }

                override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}
                override fun afterTextChanged(s: Editable) {
                    // 输入操作
                    if (s.isEmpty() && i > 0) {
                        // 删除操作
                        editTexts[i - 1]?.requestFocus() // 跳转到上一个输入框
                    } else if (s.length == 1 && i < passwordLength - 1) {
                        editTexts[i + 1]?.requestFocus() // 跳转到下一个输入框
                    } else if (s.length == 1 && i == passwordLength - 1) {
                        // 1.校验密码
                        val pwd =
                            editTexts[0]?.text.toString() + editTexts[1]?.text.toString() + editTexts[2]?.text.toString() + editTexts[3]?.text.toString()
                        // 2. 进入下一个页面
                        if (pwd == "1234") {
                            // 跳转到下一个页面
                            val intent = Intent(binding.root.context, ParentSettingActivity::class.java)
                            startActivity(intent)
                            finish() // 销毁当前页面
                        } else {
                            Toast.makeText(binding.root.context, "密码错误", Toast.LENGTH_SHORT).show()
                        }
                    }
                }
            })
            binding.llPwd.addView(editText)
        }

    }
}
